# encoding=utf8
import sys
import time
import math
reload(sys)
sys.setdefaultencoding('utf8')

class FT_0785:
    result=[]
    LatStrInfo = ""
    LongStrInfo = ""
    analysisDescription = ["普通超速","严重超速","过长怠速","急转弯","急刹车","急加速","超转行驶","空挡滑行","停车立即熄火","冷车行驶","低油量行驶","冷却系统异常","长时间刹车","长时间离合","粘离合","发动机异常熄火","停车状态踩踏油门","机油油温异常","机油压力低","运行中机油压力低","机油油压过高","电瓶电压高","电瓶电压低","蓄电瓶低压异常","蓄电瓶高压异常","猛踩油门","燃油温度过高","下坡空档滑行","高档低速","停车长时间开空调","预留","安全带未系","转弯未打转向灯","远灯使用不当","低档高速","疲劳驾驶","带手刹行驶","带故障行驶"]

    def LogicAisle(self, protocolStr, header):
        count = 0
        s = protocolStr[count:count + 4]
        n = int(s,16)
        header.append("自定义消息体长度：" + str(n) + "\n")
        count = count + 4
        s = protocolStr[count:count + 2]
        k = int(s, 16)
        header.append("数据项总数：" + str(k) + "\n")
        count = count + 2
        for i in range(k):
            header.append("\n" + "======第" + str(k) + "个位置信息======" + "\n")
            s = protocolStr[count:count + 4]
            d = s
            id = int(d, 16)
            header.append("数据ID：" + str(id) + "\n")
            count = count + 4
            s = protocolStr[count:count + 2]
            j = int(s, 16)
            header.append("数据长度：" + str(j) + "\n")
            count = count + 2
            s = protocolStr[count:count + j*2]
            header.append("数据内容：" )
            print(s)
            print(id)
            self.driveaction(id,s,header)
            count = count + j*2
        self.result = header


    def driveaction(self,id,sk,header):
        count=0
        if id==1:
            header.append("普通超速" + "\n")
            st=sk[count:count + 8]
            timestamp=int(st,16)
            # 转换成localtime
            time_local = time.localtime(timestamp)
            # 转换成新的时间格式(2016-05-05 20:28:54)
            dt = time.strftime("%Y-%m-%d %H:%M:%S", time_local)
            header.append("开始时间：" + str(dt)+"\n")
            count=count+8
            st = sk[count:count + 8]
            timestamp = int(st, 16)
            # 转换成localtime
            time_local = time.localtime(timestamp)
            # 转换成新的时间格式(2016-05-05 20:28:54)
            dt = time.strftime("%Y-%m-%d %H:%M:%S", time_local)
            header.append("结束时间：" + str(dt) + "\n")
            count = count + 8
            st = sk[count:count + 8]
            self.AnalysisLong(st)
            header.append( "开始经度："+ self.LongStrInfo + "\n")
            count = count + 8
            st = sk[count:count + 8]
            self.AnalysisLat(st)
            header.append( "开始纬度："+ self.LatStrInfo + "\n")
            count = count + 8
            st = sk[count:count + 8]
            self.AnalysisLong(st)
            header.append( "结束经度："+ self.LongStrInfo + "\n")
            count = count + 8
            st = sk[count:count + 8]
            self.AnalysisLat(st)
            header.append("结束纬度：" + self.LatStrInfo + "\n")
            count = count + 8
            st = sk[count:count + 8]
            speed = self.speedfast(st)
            header.append("最小超速值：" + str(speed) + "公里\小时"+"\n")
            count = count + 8
            st = sk[count:count + 8]
            speed = self.speedfast(st)
            header.append("最大超速值：" + str(speed) + "公里\小时"+"\n")
            count = count + 8
            st = sk[count:count + 4]
            mileage=int(st,16)
            header.append("持续里程：" + str(mileage) +"米"+ "\n")
            count = count + 8
        if id==2:
            header.append("严重超速" + "\n")
            st=sk[count:count + 8]
            timestamp=int(st,16)
            # 转换成localtime
            time_local = time.localtime(timestamp)
            # 转换成新的时间格式(2016-05-05 20:28:54)
            dt = time.strftime("%Y-%m-%d %H:%M:%S", time_local)
            header.append("开始时间：" + str(dt)+"\n")
            count=count+8
            st = sk[count:count + 8]
            timestamp = int(st, 16)
            # 转换成localtime
            time_local = time.localtime(timestamp)
            # 转换成新的时间格式(2016-05-05 20:28:54)
            dt = time.strftime("%Y-%m-%d %H:%M:%S", time_local)
            header.append("结束时间：" + str(dt) + "\n")
            count = count + 8
            st = sk[count:count + 8]
            self.AnalysisLong(st)
            header.append( "开始经度："+ self.LongStrInfo + "\n")
            count = count + 8
            st = sk[count:count + 8]
            self.AnalysisLat(st)
            header.append( "开始纬度："+ self.LatStrInfo + "\n")
            count = count + 8
            st = sk[count:count + 8]
            self.AnalysisLong(st)
            header.append( "结束经度："+ self.LongStrInfo + "\n")
            count = count + 8
            st = sk[count:count + 8]
            self.AnalysisLat(st)
            header.append("结束纬度：" + self.LatStrInfo + "\n")
            count = count + 8
            st = sk[count:count + 8]
            speed = self.speedfast(st)
            header.append("最小超速值：" + str(speed) + "公里\小时"+"\n")
            count = count + 8
            st = sk[count:count + 8]
            speed = self.speedfast(st)
            header.append("最大超速值：" + str(speed) + "公里\小时"+"\n")
            count = count + 8
            st = sk[count:count + 4]
            mileage=int(st,16)
            header.append("持续里程：" + str(mileage) +"米"+ "\n")
            count = count + 8
        if id==3:
            header.append("过长怠速" + "\n")
            st=sk[count:count + 8]
            timestamp=int(st,16)
            # 转换成localtime
            time_local = time.localtime(timestamp)
            # 转换成新的时间格式(2016-05-05 20:28:54)
            dt = time.strftime("%Y-%m-%d %H:%M:%S", time_local)
            header.append("开始时间：" + str(dt)+"\n")
            count=count+8
            st = sk[count:count + 8]
            timestamp = int(st, 16)
            # 转换成localtime
            time_local = time.localtime(timestamp)
            # 转换成新的时间格式(2016-05-05 20:28:54)
            dt = time.strftime("%Y-%m-%d %H:%M:%S", time_local)
            header.append("结束时间：" + str(dt) + "\n")
            count = count + 8
            st = sk[count:count + 8]
            self.AnalysisLong(st)
            header.append( "开始经度："+ self.LongStrInfo + "\n")
            count = count + 8
            st = sk[count:count + 8]
            self.AnalysisLat(st)
            header.append( "开始纬度："+ self.LatStrInfo + "\n")
            count = count + 8
            st = sk[count:count + 8]
            self.AnalysisLong(st)
            header.append( "结束经度："+ self.LongStrInfo + "\n")
            count = count + 8
            st = sk[count:count + 8]
            self.AnalysisLat(st)
            header.append("结束纬度：" + self.LatStrInfo + "\n")
            count = count + 8
        if id==4:
            header.append("急转弯" + "\n")
            st=sk[count:count + 8]
            timestamp=int(st,16)
            # 转换成localtime
            time_local = time.localtime(timestamp)
            # 转换成新的时间格式(2016-05-05 20:28:54)
            dt = time.strftime("%Y-%m-%d %H:%M:%S", time_local)
            header.append("开始时间：" + str(dt)+"\n")
            count=count+8
            st = sk[count:count + 8]
            timestamp = int(st, 16)
            # 转换成localtime
            time_local = time.localtime(timestamp)
            # 转换成新的时间格式(2016-05-05 20:28:54)
            dt = time.strftime("%Y-%m-%d %H:%M:%S", time_local)
            header.append("结束时间：" + str(dt) + "\n")
            count = count + 8
            st = sk[count:count + 8]
            self.AnalysisLong(st)
            header.append( "开始经度："+ self.LongStrInfo + "\n")
            count = count + 8
            st = sk[count:count + 8]
            self.AnalysisLat(st)
            header.append( "开始纬度："+ self.LatStrInfo + "\n")
            count = count + 8
            st = sk[count:count + 8]
            self.AnalysisLong(st)
            header.append( "结束经度："+ self.LongStrInfo + "\n")
            count = count + 8
            st = sk[count:count + 8]
            self.AnalysisLat(st)
            header.append("结束纬度：" + self.LatStrInfo + "\n")
            count = count + 8
            st = sk[count:count + 8]
            aspeed = self.anglespeed(st)
            header.append("最小角速度：" + str(aspeed) + "度/秒"+"\n")
            count = count + 8
            st = sk[count:count + 8]
            aspeed = self.anglespeed(st)
            header.append("最大角速度：" + str(aspeed) + "度/秒"+"\n")
            count = count + 8
        if id==5:
            header.append("急刹车" + "\n")
            st=sk[count:count + 8]
            timestamp=int(st,16)
            # 转换成localtime
            time_local = time.localtime(timestamp)
            # 转换成新的时间格式(2016-05-05 20:28:54)
            dt = time.strftime("%Y-%m-%d %H:%M:%S", time_local)
            header.append("开始时间：" + str(dt)+"\n")
            count=count+8
            st = sk[count:count + 8]
            timestamp = int(st, 16)
            # 转换成localtime
            time_local = time.localtime(timestamp)
            # 转换成新的时间格式(2016-05-05 20:28:54)
            dt = time.strftime("%Y-%m-%d %H:%M:%S", time_local)
            header.append("结束时间：" + str(dt) + "\n")
            count = count + 8
            st = sk[count:count + 8]
            self.AnalysisLong(st)
            header.append( "开始经度："+ self.LongStrInfo + "\n")
            count = count + 8
            st = sk[count:count + 8]
            self.AnalysisLat(st)
            header.append( "开始纬度："+ self.LatStrInfo + "\n")
            count = count + 8
            st = sk[count:count + 8]
            self.AnalysisLong(st)
            header.append( "结束经度："+ self.LongStrInfo + "\n")
            count = count + 8
            st = sk[count:count + 8]
            self.AnalysisLat(st)
            header.append("结束纬度：" + self.LatStrInfo + "\n")
            count = count + 8
            st = sk[count:count + 8]
            speed = self.speedfast(st)
            header.append("最大速度：" + str(speed) + "公里\小时"+"\n")
            count = count + 8
            st = sk[count:count + 8]
            speed = self.speedfast(st)
            header.append("最小速度：" + str(speed) + "公里\小时"+"\n")
            count = count + 8
            st = sk[count:count + 4]
            mileage=int(st,16)
            header.append("持续里程：" + str(mileage) +"米"+ "\n")
            count = count + 8
        if id==6:
            header.append("急加速" + "\n")
            st=sk[count:count + 8]
            timestamp=int(st,16)
            # 转换成localtime
            time_local = time.localtime(timestamp)
            # 转换成新的时间格式(2016-05-05 20:28:54)
            dt = time.strftime("%Y-%m-%d %H:%M:%S", time_local)
            header.append("开始时间：" + str(dt)+"\n")
            count=count+8
            st = sk[count:count + 8]
            timestamp = int(st, 16)
            # 转换成localtime
            time_local = time.localtime(timestamp)
            # 转换成新的时间格式(2016-05-05 20:28:54)
            dt = time.strftime("%Y-%m-%d %H:%M:%S", time_local)
            header.append("结束时间：" + str(dt) + "\n")
            count = count + 8
            st = sk[count:count + 8]
            self.AnalysisLong(st)
            header.append( "开始经度："+ self.LongStrInfo + "\n")
            count = count + 8
            st = sk[count:count + 8]
            self.AnalysisLat(st)
            header.append( "开始纬度："+ self.LatStrInfo + "\n")
            count = count + 8
            st = sk[count:count + 8]
            self.AnalysisLong(st)
            header.append( "结束经度："+ self.LongStrInfo + "\n")
            count = count + 8
            st = sk[count:count + 8]
            self.AnalysisLat(st)
            header.append("结束纬度：" + self.LatStrInfo + "\n")
            count = count + 8
            st = sk[count:count + 8]
            speed = self.speedfast(st)
            header.append("最小超速值：" + str(speed) + "公里\小时"+"\n")
            count = count + 8
            st = sk[count:count + 8]
            speed = self.speedfast(st)
            header.append("最大超速值：" + str(speed) + "公里\小时"+"\n")
            count = count + 8
            st = sk[count:count + 4]
            mileage=int(st,16)
            header.append("持续里程：" + str(mileage) +"米"+ "\n")
            count = count + 8
        if id==7:
            header.append("超转行驶" + "\n")
            st=sk[count:count + 8]
            timestamp=int(st,16)
            # 转换成localtime
            time_local = time.localtime(timestamp)
            # 转换成新的时间格式(2016-05-05 20:28:54)
            dt = time.strftime("%Y-%m-%d %H:%M:%S", time_local)
            header.append("开始时间：" + str(dt)+"\n")
            count=count+8
            st = sk[count:count + 8]
            timestamp = int(st, 16)
            # 转换成localtime
            time_local = time.localtime(timestamp)
            # 转换成新的时间格式(2016-05-05 20:28:54)
            dt = time.strftime("%Y-%m-%d %H:%M:%S", time_local)
            header.append("结束时间：" + str(dt) + "\n")
            count = count + 8
            st = sk[count:count + 8]
            self.AnalysisLong(st)
            header.append( "开始经度："+ self.LongStrInfo + "\n")
            count = count + 8
            st = sk[count:count + 8]
            self.AnalysisLat(st)
            header.append( "开始纬度："+ self.LatStrInfo + "\n")
            count = count + 8
            st = sk[count:count + 8]
            self.AnalysisLong(st)
            header.append( "结束经度："+ self.LongStrInfo + "\n")
            count = count + 8
            st = sk[count:count + 8]
            self.AnalysisLat(st)
            header.append("结束纬度：" + self.LatStrInfo + "\n")
            count = count + 8
            st = sk[count:count + 8]
            aspeed = self.valusespeed(st)
            header.append("最小转速值：" + str(aspeed) + "转/分钟"+"\n")
            count = count + 8
            st = sk[count:count + 8]
            aspeed = self. valusespeed(st)
            header.append("最大转速值：" + str(aspeed) + "转/分钟"+"\n")
            count = count + 8.

        if (id==8 or id==10 or id==11 or id==12 or id==13 or id==14 or id==15 or id==18 or id==19 or id==20 or id==22 or id==23 or id==24  or id==25 or id==26 or id==27 or id==28 or id==29 or id==32 or id==33 or id==34 or id==35 or id==36 or id==37 or id==38 ):
            header.append( self.analysisDescription[id-1]+ "\n")
            st=sk[count:count + 8]
            timestamp=int(st,16)
            # 转换成localtime
            time_local = time.localtime(timestamp)
            # 转换成新的时间格式(2016-05-05 20:28:54)
            dt = time.strftime("%Y-%m-%d %H:%M:%S", time_local)
            header.append("开始时间：" + str(dt)+"\n")
            count=count+8
            st = sk[count:count + 8]
            timestamp = int(st, 16)
            # 转换成localtime
            time_local = time.localtime(timestamp)
            # 转换成新的时间格式(2016-05-05 20:28:54)
            dt = time.strftime("%Y-%m-%d %H:%M:%S", time_local)
            header.append("结束时间：" + str(dt) + "\n")
            count = count + 8
            st = sk[count:count + 8]
            self.AnalysisLong(st)
            header.append( "开始经度："+ self.LongStrInfo + "\n")
            count = count + 8
            st = sk[count:count + 8]
            self.AnalysisLat(st)
            header.append( "开始纬度："+ self.LatStrInfo + "\n")
            count = count + 8
            st = sk[count:count + 8]
            self.AnalysisLong(st)
            header.append( "结束经度："+ self.LongStrInfo + "\n")
            count = count + 8
            st = sk[count:count + 8]
            self.AnalysisLat(st)
            header.append("结束纬度：" + self.LatStrInfo + "\n")
            count = count + 8
            st = sk[count:count + 4]
            mileage=int(st,16)
            header.append("持续里程：" + str(mileage) +"米"+ "\n")
            count = count + 8

        if id==9:
            header.append("停车立即熄火" + "\n")
            st=sk[count:count + 8]
            timestamp=int(st,16)
            # 转换成localtime
            time_local = time.localtime(timestamp)
            # 转换成新的时间格式(2016-05-05 20:28:54)
            dt = time.strftime("%Y-%m-%d %H:%M:%S", time_local)
            header.append("发生时间：" + str(dt)+"\n")
            count=count+8
            st = sk[count:count + 8]
            self.AnalysisLong(st)
            header.append( "发生经度："+ self.LongStrInfo + "\n")
            count = count + 8
            st = sk[count:count + 8]
            self.AnalysisLat(st)
            header.append( "发生纬度："+ self.LatStrInfo + "\n")
            count = count + 8



        if id == 16:
            header.append("发动机异常熄火" + "\n")
            st = sk[count:count + 8]
            timestamp = int(st, 16)
            # 转换成localtime
            time_local = time.localtime(timestamp)
            # 转换成新的时间格式(2016-05-05 20:28:54)
            dt = time.strftime("%Y-%m-%d %H:%M:%S", time_local)
            header.append("发生时间：" + str(dt) + "\n")
            count = count + 8
            st = sk[count:count + 8]
            self.AnalysisLong(st)
            header.append("发生经度：" + self.LongStrInfo + "\n")
            count = count + 8
            st = sk[count:count + 8]
            self.AnalysisLat(st)
            header.append("发生纬度：" + self.LatStrInfo + "\n")
            count = count + 8
        if id == 17:
            header.append("停车状态踩踏油门" + "\n")
            st = sk[count:count + 8]
            timestamp = int(st, 16)
            # 转换成localtime
            time_local = time.localtime(timestamp)
            # 转换成新的时间格式(2016-05-05 20:28:54)
            dt = time.strftime("%Y-%m-%d %H:%M:%S", time_local)
            header.append("开始时间：" + str(dt) + "\n")
            count = count + 8
            st = sk[count:count + 8]
            timestamp = int(st, 16)
            # 转换成localtime
            time_local = time.localtime(timestamp)
            # 转换成新的时间格式(2016-05-05 20:28:54)
            dt = time.strftime("%Y-%m-%d %H:%M:%S", time_local)
            header.append("结束时间：" + str(dt) + "\n")
            count = count + 8
            st = sk[count:count + 8]
            self.AnalysisLong(st)
            header.append("开始经度：" + self.LongStrInfo + "\n")
            count = count + 8
            st = sk[count:count + 8]
            self.AnalysisLat(st)
            header.append("开始纬度：" + self.LatStrInfo + "\n")
            count = count + 8
            st = sk[count:count + 8]
            self.AnalysisLong(st)
            header.append("结束经度：" + self.LongStrInfo + "\n")
            count = count + 8
            st = sk[count:count + 8]
            self.AnalysisLat(st)
            header.append("结束纬度：" + self.LatStrInfo + "\n")
            count = count + 8
        if id == 30:
            header.append("停车长时间空调" + "\n")
            st = sk[count:count + 8]
            timestamp = int(st, 16)
            # 转换成localtime
            time_local = time.localtime(timestamp)
            # 转换成新的时间格式(2016-05-05 20:28:54)
            dt = time.strftime("%Y-%m-%d %H:%M:%S", time_local)
            header.append("开始时间：" + str(dt) + "\n")
            count = count + 8
            st = sk[count:count + 8]
            timestamp = int(st, 16)
            # 转换成localtime
            time_local = time.localtime(timestamp)
            # 转换成新的时间格式(2016-05-05 20:28:54)
            dt = time.strftime("%Y-%m-%d %H:%M:%S", time_local)
            header.append("结束时间：" + str(dt) + "\n")
            count = count + 8
            st = sk[count:count + 8]
            self.AnalysisLong(st)
            header.append("开始经度：" + self.LongStrInfo + "\n")
            count = count + 8
            st = sk[count:count + 8]
            self.AnalysisLat(st)
            header.append("开始纬度：" + self.LatStrInfo + "\n")
            count = count + 8
            st = sk[count:count + 8]
            self.AnalysisLong(st)
            header.append("结束经度：" + self.LongStrInfo + "\n")
            count = count + 8
            st = sk[count:count + 8]
            self.AnalysisLat(st)
            header.append("结束纬度：" + self.LatStrInfo + "\n")
            count = count + 8














    def speedfast(self,source):
        speed=int(source,16)*0.1
        return speed
    def valusespeed(self,source):
        speed=int(source,16)
        return speed

    def anglespeed(self,source):
        speed=int(source,16)*0.00875
        return speed



    def AnalysisLat(self, source):
        #double Lat;
        try:
            Lat = int(source,16) * math.pow(10,-6);
            #self.LatStrInfo = String.format("%.6f", Lat);
            self.LatStrInfo = "%.6f"%Lat
        except:
            pass
    def AnalysisLong(self, source):
        #double Long;
        try:
            Long = int(source,16) * math.pow(10,-6);
            #self.LongStrInfo = String.format("%.6f", Long);
            self.LongStrInfo = "%.6f"%Long
        except:
            pass








